package ru.isys.yoomee.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;

public class Chat implements Serializable
{
    String chatName;
    String text;
    ArrayList<Yoo> yoos;
    Yoo owner;
    ArrayList<ChatItem> chatItems;

    @Deprecated
    public Chat(String chatName, String text)
    {
        this.chatName = chatName;
        this.text = text;
        this.yoos = new ArrayList<Yoo>();
    }

    @Deprecated
    public Chat(String nickName)
    {
        this(nickName, "");
    }

    public Chat(Yoo owner, ArrayList<Yoo> yoos)
    {
        this.owner = owner;
        this.yoos = yoos;
        this.chatName = prepareChatName(this.yoos);
    }
    
    public Chat (Yoo owner, Yoo yoo){
        this.owner = owner;        
        this.yoos =new ArrayList<Yoo>();
        this.yoos.add(yoo);
        this.chatName=prepareChatName(this.yoos);
    }
    
    public Chat (Yoo owner , Yoo...yoos ){
        this.owner = owner;        
        this.yoos =new ArrayList<Yoo>(Arrays.asList(yoos));        
        this.chatName=prepareChatName(this.yoos);
    }

    private String prepareChatName(ArrayList<Yoo> yoos)
    {
        StringBuilder chatNameBuilder = new StringBuilder();
        if (yoos != null)
        {
            for (Yoo yoo : yoos)
            {
                if (yoo != yoos.get(yoos.size() - 1))
                {
                    chatNameBuilder.append(yoo.getNickName());
                    chatNameBuilder.append(", ");
                }
                else
                {
                    chatNameBuilder.append(yoo.getNickName());
                }
            }
            return chatNameBuilder.toString();
        }
        return null;
    }

    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
    }

    public ArrayList<Yoo> getYoos()
    {
        return yoos;
    }

    public void setYoos(ArrayList<Yoo> yoos)
    {
        this.yoos = yoos;
    }

    public void addYoo(Yoo yoo)
    {
        this.yoos.add(yoo);
    }

    public void removeYoo(Yoo yoo)
    {
        this.yoos.remove(yoo);
    }   

    public ArrayList<ChatItem> getChatItems()
    {
        return chatItems;
    }

    public void setChatItems(ArrayList<ChatItem> chatItems)
    {
        this.chatItems = chatItems;
    }

    public void addChatItem(ChatItem chatItem)
    {
        this.chatItems.add(chatItem);
    }

    public void removeChatItem(ChatItem chatItem)
    {
        this.chatItems.remove(chatItem);
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((chatItems == null) ? 0 : chatItems.hashCode());
        result = prime * result + ((chatName == null) ? 0 : chatName.hashCode());
        result = prime * result + ((owner == null) ? 0 : owner.hashCode());
        result = prime * result + ((text == null) ? 0 : text.hashCode());
        result = prime * result + ((yoos == null) ? 0 : yoos.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Chat other = (Chat) obj;
        if (chatItems == null)
        {
            if (other.chatItems != null)
                return false;
        }
        else if (!chatItems.equals(other.chatItems))
            return false;
        if (chatName == null)
        {
            if (other.chatName != null)
                return false;
        }
        else if (!chatName.equals(other.chatName))
            return false;
        if (owner == null)
        {
            if (other.owner != null)
                return false;
        }
        else if (!owner.equals(other.owner))
            return false;
        if (text == null)
        {
            if (other.text != null)
                return false;
        }
        else if (!text.equals(other.text))
            return false;
        if (yoos == null)
        {
            if (other.yoos != null)
                return false;
        }
        else if (!yoos.equals(other.yoos))
            return false;
        return true;
    }

    public String getChatName()
    {
        return chatName;
    }

    public void setChatName(String chatName)
    {
        this.chatName = chatName;
    }

    public Yoo getOwner()
    {
        return owner;
    }

    public void setOwner(Yoo owner)
    {
        this.owner = owner;
    }

}
